package winOrDie.graphicsInterface.GameRoom;

import java.util.ArrayList;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import winOrDie.Game.WoD_Game;
import winOrDie.Net.Client.WoD_Peer;
import winOrDie.graphicsInterface.WoD_Composite_Type;
import winOrDie.graphicsInterface.GameMenu.Composite_List_Games_Event;

public class Composite_Maps_List extends Composite {

	int compType;
	private Table table = null;
	public String SelectedMap = null;

public String getSelectedMap() {
		return SelectedMap;
	}

	public void setSelectedMap(String selectedMap) {
		SelectedMap = selectedMap;
	}


private Vector<Composite_Maps_Listener> listeners = null;
	
	public synchronized void add_Composite_Maps_Listener(Composite_Maps_Listener listener)
	{
		listeners.add(listener);
	}
	
	public synchronized void remove_Composite_Maps_Listener(Composite_Maps_Listener listener)
	{
		listeners.remove(listener);
	}
	
	public void fire_Composite_Maps_Event(Composite_Maps_Event event)
	{
		for (Composite_Maps_Listener listener : listeners) {
			listener.on_Composite_Maps_Changed(event);
		}
	}
	
	public Composite_Maps_List(Composite parent, int style, int compType) {
		super(parent, style);
		this.compType = compType;
		initialize();
//		test();
	}

	private void initialize() {
		
		setSize(new Point(100, 200));
		setLayout(new GridLayout());
		listeners = new Vector<Composite_Maps_Listener>();
		get_Table();
	}

	
	private Table get_Table()
	{
		if (table == null)
		{
			GridData gridData = new GridData();
			gridData.grabExcessHorizontalSpace = true;
			gridData.grabExcessVerticalSpace = true;
			gridData.verticalAlignment = SWT.FILL;
			gridData.horizontalAlignment = SWT.FILL;
			
			
			table = new Table(this, SWT.BORDER);
			table.setHeaderVisible(false);
			table.setLinesVisible(false);
			table.setLayoutData(gridData);
			
			table.addListener(SWT.DefaultSelection, new Listener() {
                public void handleEvent (Event e) {
                	Composite_Maps_Event event = new Composite_Maps_Event(this);
					event.setEventType(Composite_Maps_Event.Event_Types.MAP_SELECTED);
					
					for(int i = 0; i<table.getItems().length; i++)
					{
						TableItem item = table.getItem(i);

						String e_string = e.item.toString().replace("TableItem {", "");
						e_string = e_string.replace("}", "");
						
						if(e_string.equals(item.getText(0)))
						{
							setSelectedMap(item.getText(0));							
						}
					}					
					System.out.println("default "+e.item);
					fire_Composite_Maps_Event(event);					
                }
			});
		}
		
		return table;
	}
	

	public void paint_Table(final ArrayList<String> Maps)
	{
		if (table.isDisposed()) {return;}
		Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				
				getDisplay().syncExec(new Runnable() {
					
					@Override
					public void run() {
						if (table.isDisposed()) {return;}
						table.removeAll();
					}
				});
				
								
				for (final String map : Maps) {
					if (isDisposed()) return;
					getDisplay().syncExec(new Runnable() {
						
						@Override
						public void run() {

							
							TableItem item = new TableItem(table, SWT.NONE);
							item.setText(map);

						}
					});
					
				}
				
			}
		};
		
		new Thread(runnable).start(); 
	}
	
	
}
